package com.yanggu.spock.util

import spock.lang.Specification
import spock.lang.Unroll

class IDNumberUtilsTest extends Specification {

    @Unroll
    def '身份证号: #idNo, 生日: #expectedBirthday, 性别: #expectedSex'() {
        when: "when + then 组合"
        def result = IDNumberUtils.getBirAgeSex(idNo)

        then: "验证结果是否符合预期值"
        result.birthday == expectedBirthday
        result.sex == expectedSex
        result.age == calculateAge(expectedBirthday)

        where: "定义表格数据"
        idNo                 || expectedBirthday | expectedSex
        "310168199809187333" || "1998-09-18"     | "男"
        "320168200212084268" || "2002-12-08"     | "女"
        "330168199301214267" || "1993-01-21"     | "女"
        "411281870628201"    || "1987-06-28"     | "男"
        "427281730307862"    || "1973-03-07"     | "女"
        "479281691111377"    || "1969-11-11"     | "男"
    }

    //根据出生日期，计算年龄
    //"1969-11-11"格式
    def calculateAge(String birthday) {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        return Integer.valueOf(year - Integer.parseInt(birthday.substring(0, 4))).toString()
    }

}